package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Xiao
 * @date 2021/9/29
 */
@Service
@Slf4j
public class ShoppingCartImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    /**
     * 获取购物车数据
     * @return
     */
    @Override
    public List<ShoppingCart> cartListApi() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        wrapper.orderByDesc(ShoppingCart::getCreateTime);
        return this.list(wrapper);
    }
    /**
     * 添加到购物车
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart addCartApi(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        if (shoppingCart.getDishId() != null) {
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCart1 = this.getOne(wrapper);
        if (shoppingCart1 != null) {
            Integer number = shoppingCart1.getNumber();
            shoppingCart1.setNumber(number + 1);
            this.updateById(shoppingCart1);
        } else {
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            this.save(shoppingCart);
            shoppingCart1 = shoppingCart;
        }
        return shoppingCart1;
    }

    /*@Override
    public boolean addCartApi(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getName, shoppingCart.getName());
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        ShoppingCart shoppingCart1 = this.getOne(wrapper);
        if (shoppingCart1 != null) {
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            return this.updateById(shoppingCart1);
        }
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        return this.save(shoppingCart);
    }*/

    /**
     * 从购物车减去菜品/套餐
     * @param dishId
     * @param setmealId
     * @return
     */
    @Override
    public ShoppingCart updateCartApi(Long dishId, Long setmealId) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        wrapper.eq(dishId != null, ShoppingCart::getDishId, dishId);
        wrapper.eq(setmealId != null, ShoppingCart::getSetmealId, setmealId);
        ShoppingCart shoppingCart = this.getOne(wrapper);
        int count = shoppingCart.getNumber();
        if (count > 1) {
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            this.updateById(shoppingCart);
        } else {
            this.removeById(shoppingCart.getId());
            shoppingCart = new ShoppingCart();
            shoppingCart.setNumber(0);
        }
        return shoppingCart;
    }

    /*@Override
    public boolean updateCartApi(Long dishId, Long setmealId) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        wrapper.eq(dishId != null, ShoppingCart::getDishId, dishId);
        wrapper.eq(setmealId != null, ShoppingCart::getSetmealId, setmealId);
        ShoppingCart shoppingCart = this.getOne(wrapper);
        int count = shoppingCart.getNumber();
        if (count > 1) {
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            return this.updateById(shoppingCart);
        }
        return this.removeById(shoppingCart.getId());
    }*/

    /**
     * 清空购物车
     * @return
     */
    @Override
    public boolean clearCartApi() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        return this.remove(wrapper);
    }

    /**
     * 再来一单
     * @param shoppingCarts
     * @return
     */
    @Override
    public boolean orderAgainApi(List<ShoppingCart> shoppingCarts) {
        this.clearCartApi();
        return this.saveBatch(shoppingCarts);
    }
}
